import json
from datetime import datetime

from django.http import JsonResponse

from lapi_app.models.task_case.task import Task_result
from lapi_app.models.testcase_model.testcase import Testcase


def get_task_result(request):
    try:
        task_result = []
        task_list = []
        data = json.loads(request.body)
        task_id = data["task_id"]
        tasks = Task_result.objects.filter(task_id=task_id)
        for task in tasks:
            task_case = task.case_id
            task_name = task.task.task_name
            task_list.append(task_case)
            for t in task_list:
                cases = Testcase.objects.filter(id=t)
            for case in cases:
                case_dict = {
                    "from_task": task_name,
                    "case_id": case.id,
                    "name": case.t_name,
                    "method": case.t_method,
                    "url": case.t_url,
                    "task_status": task.task_status,
                    "api_runtime": task.task_res_time,
                    "task_runtime": datetime.strftime(task.task_runtime, "%Y-%m-%d %H:%M:%S")
                }
                task_result.append(case_dict)

        return JsonResponse({
            "code": 200,
            "message": "获取任务结果成功",
            "content": task_result
        })
    except Exception as e:
        return JsonResponse({
            "code": 100,
            "message": "获取任务结果失败",
            "Exception": str(e)
        })


def get_task_result_details(request):
    try:
        data = json.loads(request.body)
        case_id = data["case_id"]
        task_id = data["task_id"]
        result = []
        cases = Testcase.objects.filter(id=case_id)
        case_results = Task_result.objects.filter(case_id=case_id, task_id=task_id)
        for case in cases:
            case_host = case.t_environment.e_address
            case_url = case.t_url
            req_url = case_host + case_url
            params = {}
            if not case.t_params:
                for item in case.t_params:
                    if item['key'] == '' or item['value'] == '':
                        params = {}
                    else:
                        params[item['key']] = item['value']
            else:
                params = {}
            for case_result in case_results:
                result_dict = {
                    "req_url": req_url,
                    "headers": case.t_header,
                    "params": params,
                    "body": case.t_body,
                    "req_result": case_result.task_result,
                    "assert_data": case_result.task_assert,
                    "extract_result": case_result.task_extract,
                    "res_runtime": case_result.task_res_time
                }
                result.append(result_dict)
            return JsonResponse({
                "code": 200,
                "message": "获取结果成功",
                "content": result
            })
    except Exception as e:
        return JsonResponse({
                "code": 100,
                "message": "获取结果失败",
                "Exception": str(e)
            })